package logic

import (
	"context"

	"mqtt/internal/svc"
	"mqtt/mqtt"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListSubscribedTopicsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListSubscribedTopicsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListSubscribedTopicsLogic {
	return &ListSubscribedTopicsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 获取已订阅的主题
func (l *ListSubscribedTopicsLogic) ListSubscribedTopics(in *mqtt.ListTopicsRequest) (*mqtt.ListTopicsResponse, error) {

	// 调用客户端管理器获取已订阅的主题
	topics, err := l.svcCtx.ClientMgr.ListSubscribedTopics(in.ServerAddr)
	if err != nil {
		return &mqtt.ListTopicsResponse{
			Topics: nil,
			Error:  err.Error(),
		}, nil
	}

	return &mqtt.ListTopicsResponse{
		Topics: topics,
	}, nil
}
